package com.atbun.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.atbun.vod.service.VodService;
import com.atbun.vod.utils.AliyunVodSDKUtils;
import com.atbun.vod.utils.ConstantPropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Description :
 * @Author : mayp
 * @Date: 2020-05-11 22:28
 */
@Service
public class VodServiceImpl implements VodService {

    /**
     * 上传视频到阿里云服务器
     * @param file
     * @return
     */
    @Override
    public String uploadVideo(MultipartFile file) {
        try{
            String fileName = file.getOriginalFilename(); // 本地视频的名字
            String title = fileName.substring(0,fileName.lastIndexOf(".")); // 阿里云视频显示的名字 因为本地有如 01.02.mp4 那么取到的是最后一个点前面的名字 01.02
            InputStream inputStream = file.getInputStream(); // 流式上传

            UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

//            返回videoId
            String videoId = response.getVideoId();
//            如果上传错误
            if (!response.isSuccess()) {
                if(StringUtils.isEmpty(videoId)){
                    throw new GuliException(20001, "上传视频到阿里云失败");
                }
            }
            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001, "atbun vod 服务上传失败");
        }

    }

    /**
     * 根据云端视频id删除单个视频
     * @param videoId
     */
    @Override
    public void deleteVideoByVideoId(String videoId) {
        try{
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
//            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (ClientException  e){
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }

    /**
     * 删除多个视频
     * @param videoIdList
     */
    @Override
    public void removeMoreAlyVideo(List videoIdList) {
        try{
//            初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
//            创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
//            videoIdList转换成 1,2,3
            String videoIds = StringUtils.join(videoIdList.toArray(),",");
//            向request中设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }


}
